package com.wulaobo.transformer;

import com.wulaobo.pojo.WaterSensor;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @Author wuwenbo
 * @Date 2024/5/17 10:31
 * @ClassName: MapDemo
 * @Description: TODO
 */
public class FlatmapDemo {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        DataStreamSource<WaterSensor> stream = env.fromElements(
                new WaterSensor("sensor_1", 1, 1),
                new WaterSensor("sensor_2", 2, 2),
                new WaterSensor("sensor_2", 2, 3),
                new WaterSensor("sensor_4", 3, 4),
                new WaterSensor("sensor_5", 4, 5)
        );

        SingleOutputStreamOperator<Integer> singleOutputStreamOperator = stream.flatMap(new FlatMapFunction<WaterSensor, Integer>() {
            @Override
            public void flatMap(WaterSensor waterSensor, Collector<Integer> out) throws Exception {
                if ("sensor_2".equals(waterSensor.getId())) {
                    out.collect(waterSensor.getTs());
                    out.collect(waterSensor.getVc());
                } else {
                    out.collect(waterSensor.getVc());
                }
            }
        });
        singleOutputStreamOperator.print();

//        stream.flatMap((WaterSensor waterSensor, Collector<Integer> out) -> {
//            if ("sensor_2".equals(waterSensor.getId())) {
//                out.collect(waterSensor.getTs());
//                out.collect(waterSensor.getVc());
//            } else {
//                out.collect(waterSensor.getVc());
//            }
//        }).returns(Types.INT).print();

        env.execute();
    }

}
